﻿using System.ComponentModel;
using System.Web;
using System.Web.Mvc;
using xTeam.HengLe.DataAccess;
using xTeam.HengLe.Entity;
using System.Collections.Generic;

namespace xTeam.HengLe.Controller.Website
{
    public class NewsController : BaseController
    {
        NewsDataAccess _newsDataAccess;
        /// <summary>
        /// 
        /// </summary>
        public NewsController()
        {
            _newsDataAccess = new NewsDataAccess(_connectionString);
        }
        /// <summary>
        /// 新闻主页
        /// </summary>
        public ActionResult Index(int classid = 0, string key = "")
        {
            return RedirectToAction("List", new { classid = classid, key = key });

        }
        /// <summary>
        /// 分类新闻
        /// </summary>
        public ActionResult List([DefaultValue(0)]int classid, string key = "")
        {
            NewsViewModel viewModel = new NewsViewModel();
            viewModel.ClassifyList = _classifyDataAccess.GetClassLisytByModuleKey("News");
            viewModel.NewsList = _newsDataAccess.Search(key, classid, true);
            viewModel.Classify = _classifyDataAccess.GetClassifyById(classid) ?? new EClassify() { Title = "News" };
            if (viewModel.NewsList.Count == 1)
                return RedirectToAction("info", new { newsId = viewModel.NewsList[0].NewsId });
            return View(viewModel);
        }
        /// <summary>
        /// 新闻详细信息
        /// </summary>
        public ActionResult info([DefaultValue(0)]int newsId)
        {
            ENews news = _newsDataAccess.GetNewsById(newsId, true);
            if (news == null)
                throw new HttpException(404, "Not Found !");

            NewsViewModel viewModel = new NewsViewModel();
            viewModel.NewsInfo = news;
            viewModel.Classify = _classifyDataAccess.GetClassifyById(news.ClassifyId);
            // 站点路径
            List<EClassify> pathList = _classifyDataAccess.GetParentList(news.ClassifyId, "News");
            foreach (var item in pathList)
            {
                this.SiteMapPathAdd(item.Title, Url.Action("Index", new { classid = item.ClassifyId }));
            }
            return View(viewModel);
        }
    }
}
